var api = require('../../utils/api.js');
// var echarts=require('../../ec-canvas/echarts.js');
 let charts = require('../../utils/wxcharts-min.js');
var lineChart = null


Page({

  /**
   * 页面的初始数据
   */
  data: {
    // ec: {onInit: ''},
    date1:'',
    date2: '',
    value1:"校区",
    value2:'项目',
    rangeKey1: 'campus_name',
    rangeKey2: 'project_name',
    projectArr:[],
    campusArr:[]
  },


  //获取校区、项目、班级
  getSchool(campus_id = '') {
    
    api.fetchApi('principal/getbasic', {
      token: wx.getStorageSync('token')
      ,campus_id
    }).then(res => {
      var projectArr = campus_id ? res.project : [];
      var campusArr = res.campus;
      console.log(projectArr, campusArr)
      this.setData({
        projectArr,
        campusArr
      })

    })
  },

  //选择校区
  selectSchool(e) {
    var campusArr = this.data.campusArr;
    var rangeKey1 = this.data.rangeKey1;
    var nowArr = campusArr[e.detail]
    console.log(nowArr)
    this.setData({
      value1: nowArr[rangeKey1],
      campus_id: nowArr.id,
      value2: '项目'
    })
    this.getSchool(nowArr.id);
    //this.getStudentTotal()
  },

  //选择项目
  selectProject(e){
    var projectArr = this.data.projectArr;
    var rangeKey2 = this.data.rangeKey2;
    var nowArr = projectArr[e.detail]
    console.log(nowArr)
    this.setData({
      value2: nowArr[rangeKey2],
      project_id: nowArr.id
    })

    this.getStudentTotal()
  },

  //时间始   子组件传过来的事件
  dateChange1(e) {

    this.setData({
      date1: e.detail
    })
    this.getStudentTotal()

  },

  //时间止
  dateChange2(e) {

    this.setData({
      date2: e.detail
    })
    this.getStudentTotal()

  },


  //获取学生统计
  getStudentTotal() {
    var params = {
      token: wx.getStorageSync('token'),
      campus_id: this.data.campus_id,
      project_id: this.data.project_id,
      time: this.data.date1 + '~' + this.data.date2
    }
    api.fetchApi('principal/student_statistics', params).then(res => {
      var total = res.total;
      var dataArr = [];
      res.data.forEach(item => {
        dataArr.push({
          data: item.total, name: item.name, color: item.color, format: () => {
            return item.total + '人'
          }})
      })
      console.log(dataArr)
      var res1 = wx.getSystemInfoSync(),   //试图获取屏幕宽高数据
        windowWidth = res1.windowWidth ,   
        windowHeight = res1.windowWidth 
      if (dataArr.length > 0) {
        this.setData({
          zanwu: false
        })
        lineChart = new charts({
          animation: true,
          canvasId: 'canvas1',
          type: 'ring',
          extra: {
            ringWidth: 20,//圆环的宽度
            pie: {
              offsetAngle: -45//圆环的角度
            }
          },
          title: {
            name: total,
            color: '#000',
            fontSize: 25
          },
          subtitle: {
            name: '总计人数',
            color: '#474747',
            fontSize: 15
          },
          series: dataArr,
          disablePieStroke: true,
          width: windowWidth,
          height: windowHeight,
          dataLabel: true,
          legend: true,
          padding: 0
        });
        }else{
       // api.showToast('暂无数据!','http://qnwmfx.laigl.com/iconimg/warn.png')
        this.setData({
          zanwu:true
        })
        return
        }
       
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var date1 = new Date().dateFormat('Y-m-d');
    var date2 = (new Date(new Date().valueOf() + 86400000)).dateFormat('Y-m-d')
    console.log(date1, date2)
    this.setData({
      date1,
      date2
    })
    this.getSchool();
    this.getStudentTotal()
   
  },

  chooseSchool(e){
    console.log(e)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.ecComponent = this.selectComponent('#mychart-dom-bar'); 
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})